package com.live.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.live.model.Admin;
import com.live.util.DBUtil;
import com.live.util.RowMapper;

public class AdminDao {
	public int addAdmin(Admin admin){
		String sql = "insert into admin(account ,name, comments, mail , password,time,sign,special,role,landingTime) values(?,?,?,?,?,?,?,?,?,?)";
		Object[] param = {admin.getAccount(),admin.getName(),admin.getComments(),admin.getMail(),admin.getPassword(),admin.getTime(),admin.getSign(),admin.getSpecial(),admin.getRole(),admin.getLandingTime()};
		try {
			DBUtil.executeUpdate(sql, param);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	public Admin getAdmin(String account, String password) {
		Admin admin = null;
		String sql = "select  * from admin where account=? and password=?";
		Object[] param = { account, password };
		Object result = DBUtil.executeQuery(sql, new RowMapper() {
			@Override
			public Object mapper(ResultSet rs) throws SQLException {
				Admin data = null;
				while (rs.next()) {
					data = new Admin();
					data.setId(rs.getInt("id"));
					data.setAccount(rs.getString("account"));
					data.setPassword(rs.getString("password"));
					data.setComments(rs.getString("comments"));
					data.setRole(rs.getInt("role"));
				}
				return data;
			}
		}, param);
		if (result != null) {
			admin = (Admin) result;
		}
		return admin;
	}
	public Admin getAdminAccount(String account) {
		Admin admin = null;
		String sql = "select  * from admin where account=?";
		Object[] param = { account};
		Object result = DBUtil.executeQuery(sql, new RowMapper() {
			@Override
			public Object mapper(ResultSet rs) throws SQLException {
				Admin data = null;
				while (rs.next()) {
					data = new Admin();
					data.setId(rs.getInt("id"));
					data.setAccount(rs.getString("account"));
					data.setPassword(rs.getString("password"));
					data.setComments(rs.getString("comments"));
					data.setRole(rs.getInt("role"));
				}
				return data;
			}
		}, param);
		if (result != null) {
			admin = (Admin) result;
		}
		return admin;
	}
	public List<Admin> getAdminId() {
		String sql = "select id,name,mail from  admin where sign = 0  and  role = 2 and special=0 order by time desc";
		Object result = DBUtil.executeQuery(sql, new RowMapper() {
			@Override
			public Object mapper(ResultSet rs) throws SQLException {
				List<Admin> data = new ArrayList<Admin>();
				while (rs.next()) {
					Admin admin = new Admin();
					admin.setId(rs.getInt("id"));
					admin.setName(rs.getString("name"));
					admin.setMail(rs.getString("mail"));
					data.add(admin);
				}
				return data;
			}
		});
		List<Admin> list = (List<Admin>) result;
		return list;
	}
	public List<Admin> getAdminName() {
		String sql = "select  * from  admin where role = 2 ";
		Object result = DBUtil.executeQuery(sql, new RowMapper() {
			@Override
			public Object mapper(ResultSet rs) throws SQLException {
				List<Admin> data = new ArrayList<Admin>();
				while (rs.next()) {
					Admin admin = new Admin();
					admin.setId(rs.getInt("id"));
					admin.setAccount(rs.getString("account"));
					admin.setName(rs.getString("name"));
					admin.setPassword(rs.getString("password"));
					admin.setSpecial(rs.getInt("special"));
					admin.setComments(rs.getString("comments"));
					admin.setSign(rs.getInt("sign"));
					admin.setRole(rs.getInt("role"));
					admin.setTime(rs.getString("time"));
					admin.setMail(rs.getString("mail"));
					data.add(admin);
				}
				return data;
			}
		});
		List<Admin> list = (List<Admin>) result;
		return list;
	}
	
	public List<Admin> getAdminList(int pageIndex, int pageSize) {
		String sql = "select  * from  admin where role = 2 limit ?,?";
		Object[] Param = { (pageIndex - 1) * pageSize, pageSize };
		Object result = DBUtil.executeQuery(sql, new RowMapper() {
			@Override
			public Object mapper(ResultSet rs) throws SQLException {
				List<Admin> data = new ArrayList<Admin>();
				while (rs.next()) {
					Admin admin = new Admin();
					admin.setId(rs.getInt("id"));
					admin.setAccount(rs.getString("account"));
					admin.setName(rs.getString("name"));
					admin.setPassword(rs.getString("password"));
					admin.setSpecial(rs.getInt("special"));
					admin.setComments(rs.getString("comments"));
					admin.setSign(rs.getInt("sign"));
					admin.setRole(rs.getInt("role"));
					admin.setTime(rs.getString("time"));
					admin.setLandingTime(rs.getString("landingTime"));
					admin.setMail(rs.getString("mail"));
					data.add(admin);
				}
				return data;
			}
		},Param);
		List<Admin> list = (List<Admin>) result;
		return list;
	}
	public int getTotalCount() {
		String sql = "select count(*)totalcount from admin where role = 2";
		Object result = DBUtil.executeQuery(sql, new RowMapper() {
			@Override
			public Object mapper(ResultSet rs) throws SQLException {
				int totalCount = 0;
				while (rs.next()) {
					totalCount = rs.getInt("totalCount");
				}

				return totalCount;
			}
		});
		return (int) result;
	}
	public int editAdmin(Admin admin) {
		String sql = "update admin set sign=? where id=?";
		Object[] param = { admin.getSign(), admin.getId() };
		try {
			DBUtil.executeUpdate(sql, param);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	public int editAdminSpecial(Admin admin) {
		String sql = "update admin set special=? ,name=? ,mail=? where id=?";
		Object[] param = { admin.getSpecial(), admin.getName(),admin.getMail(), admin.getId() };
		try {
			DBUtil.executeUpdate(sql, param);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	public int editAdminSign(Admin admin) {
		String sql = "update admin set sign=? ";
		Object[] param = { admin.getSign()};
		try {
			DBUtil.executeUpdate(sql, param);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	public int editAdminLandingTime(Admin admin) {
		String sql = "update admin set landingTime=?  where id=?";
		Object[] param = { admin.getLandingTime(),admin.getId() };
		try {
			DBUtil.executeUpdate(sql, param);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	public int editAdminPassword(Admin admin) {
		String sql = "update admin set password=?  where id=?";
		Object[] param = { admin.getPassword(),admin.getId() };
		try {
			DBUtil.executeUpdate(sql, param);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
}
